<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="editRole()"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- 默认插槽 当前行数据 -->
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="addPerm(row)">
                    分配权限
                  </el-button>
                  <el-button size="small" type="primary" @click="editRole(row)">
                    编辑
                  </el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delCurr(row.id)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="query.pagesize"
                :current-page="query.page"
                :total="total"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <h1>黑马137</h1>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title="编辑弹层" :visible.sync="show">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="close">取消</el-button>
          <el-button size="small" type="primary" @click="addRole">
            确定
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <AssignPerm ref="perm" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
// 导入权限弹层
import AssignPerm from './components/assign-perm.vue'
export default {
  components: {
    AssignPerm
  },
  data () {
    return {
      showAssignDialog: false, // 控制权限弹层
      show: false, // 控制弹层
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      list: [],
      query: {
        page: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 点击打开分配权限
    addPerm (row) {
      this.showAssignDialog = true
      // 调用子的回显
      this.$refs.perm.getPerm(row.id)
    },
    // 点击编辑显示弹层并获取实时详情
    async editRole (currRole) {
      // console.log(currRole)
      this.show = true
      try {
        if (currRole) {
          const res = await getRoleDetail(currRole.id)
          // 新增了一个id
          // console.log(res)
          // this.roleForm = currRole
          this.roleForm = res
        } else {
          this.close()
          this.show = true
        }
      } catch (e) {
        console.log(e)
      }
    },
    // 确定新增(或编辑)
    async addRole () {
      // 校验
      try {
        this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          // 是编辑
          await updateRole(this.roleForm)
          this.$message.success('编辑成功')
        } else {
          // 是增加
          await addRole(this.roleForm)
          this.query.page = 1
          this.$message.success('添加成功')
        }
        // this.close()
        this.show = false
      } catch (e) {
        console.log(e)
      }
    },
    // 删除
    async delCurr (id) {
      try {
        await this.$confirm('确认删除吗')
        await deleteRole(id)
        this.query.page = 1
        this.getList()
        this.$message.success('删除成功')
      } catch (e) {
        console.log(e)
      }
    },
    // 取消
    close () {
      // 重置表单和校验规则
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
      this.show = false
    },
    // 刷新(默认形参)
    changePage (curr) {
      this.query.page = curr
      this.getList()
    },
    // 得到列表
    async getList () {
      const { rows, total } = await getRoleList(this.query)
      // console.log(res)
      this.list = rows
      this.total = total
    }
  }

}
</script>

<style lang="sass" scoped>
</style>
